// pages/records/records.js
const app = getApp()

Page({
  data: {
    records: [],
    loading: true,
    page: 1,
    limit: 10,
    hasMore: true,
    refreshing: false,
    totalQuantity: 0,
    totalWage: 0,
    pendingCount: 0
  },

  onLoad() {
    this.loadRecords()
  },

  onShow() {
    this.loadRecords()
  },

  onPullDownRefresh() {
    this.setData({
      refreshing: true,
      page: 1,
      hasMore: true
    })
    this.loadRecords().then(() => {
      wx.stopPullDownRefresh()
      this.setData({
        refreshing: false
      })
    })
  },

  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadMoreRecords()
    }
  },

  // 加载报工记录
  loadRecords() {
    return app.request({
      url: '/getRecords',
      data: {
        page: this.data.page,
        limit: this.data.limit
      }
    }).then((res) => {
      const records = res.data.list || []
      
      // 计算统计数据
      let totalQuantity = 0
      let totalWage = 0
      let pendingCount = 0
      
      records.forEach(record => {
        totalQuantity += parseInt(record.quantity) || 0
        totalWage += parseFloat(record.wage) || 0
        if (record.status !== 1) {
          pendingCount++
        }
      })
      
      this.setData({
        records: records,
        hasMore: records.length >= this.data.limit,
        loading: false,
        totalQuantity: totalQuantity,
        totalWage: totalWage,
        pendingCount: pendingCount
      })
    }).catch((err) => {
      console.error('获取报工记录失败:', err)
      this.setData({
        loading: false
      })
      wx.showToast({
        title: '获取记录失败',
        icon: 'none'
      })
    })
  },

  // 加载更多记录
  loadMoreRecords() {
    if (!this.data.hasMore) return

    const nextPage = this.data.page + 1
    this.setData({
      page: nextPage,
      loading: true
    })

    app.request({
      url: '/getRecords',
      data: {
        page: nextPage,
        limit: this.data.limit
      }
    }).then((res) => {
      const newRecords = [...this.data.records, ...res.data.list]
      
      // 重新计算统计数据
      let totalQuantity = 0
      let totalWage = 0
      let pendingCount = 0
      
      newRecords.forEach(record => {
        totalQuantity += parseInt(record.quantity) || 0
        totalWage += parseFloat(record.wage) || 0
        if (record.status !== 1) {
          pendingCount++
        }
      })
      
      this.setData({
        records: newRecords,
        hasMore: res.data.list.length >= this.data.limit,
        loading: false,
        totalQuantity: totalQuantity,
        totalWage: totalWage,
        pendingCount: pendingCount
      })
    }).catch((err) => {
      console.error('加载更多记录失败:', err)
      this.setData({
        loading: false
      })
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      })
    })
  },

  // 刷新记录
  refreshRecords() {
    this.setData({
      page: 1,
      hasMore: true,
      loading: true
    })
    this.loadRecords()
  },

  // 预览图片
  previewImage(e) {
    const { urls, current } = e.currentTarget.dataset
    wx.previewImage({
      urls: urls,
      current: current
    })
  },

  // 查看记录详情
  viewRecord(e) {
    const record = e.currentTarget.dataset.record
    // 可以跳转到详情页面或显示弹窗
    wx.showModal({
      title: '报工详情',
      content: `订单号：${record.order_no}\n产品：${record.product_name} - ${record.model_name}\n工序：${record.process_name}\n数量：${record.quantity}\n工价：¥${record.price}\n工资：¥${record.wage}\n状态：${record.status_text}\n时间：${record.createtime_text}`,
      showCancel: false
    })
  }
}) 